#include "Object3D.h"

/********************************************************************************************************
Constructor & Destructor
********************************************************************************************************/

Object3D::Object3D() : m_shaderColor("Shaders/couleurs.vert", "Shaders/couleurs.frag"), m_vertex(0)
{

	m_shaderColor.initialiser();

}

Object3D::Object3D(float* vertex, int nbVertex, unsigned int* index, int nbIndex):m_shaderColor("Shaders/couleurs.vert", "Shaders/couleurs.frag")
{
	
	m_shaderColor.initialiser();

	m_vertex = vertex;
	m_index = index;
	m_nbIndex = nbIndex;
	m_nbVertex = nbVertex;

	setVertexRedColor();
	
}


Object3D::~Object3D()
{
	delete[] m_vertex;
	delete[] m_index;
	delete[] m_red;
}


/********************************************************************************************************
Vertex Color
********************************************************************************************************/

void Object3D::setVertexRedColor()
{
	//Set Color on all vertex
	m_red = new float[m_nbVertex * 3];

	for(int i = 0; i < m_nbVertex * 3; i++){
		m_red[i++] = 1.0f;
		m_red[i++] = 0.0f;
		m_red[i] = 0.0f;
	}
}

/********************************************************************************************************
Draw
********************************************************************************************************/

//Draw the object
void Object3D::draw(Matrice &modelview, Matrice &projection)
{
	modelview.push();

	//Shader & Vertex Init
	glUseProgram(m_shaderColor.getProgramID());
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	//Shader Values
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "projection"), 1, GL_TRUE, projection.getValues());
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "modelview"), 1, GL_TRUE, modelview.getValues());

	//Draw Object
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, m_red);
	glDrawElements(GL_TRIANGLES, m_nbIndex, GL_UNSIGNED_INT, m_index);

	/*
	//Draw Vertex
	glBegin(GL_POINTS);
	
	for(int i = 0; i < m_nbVertex; i++){
		glVertex3f(m_vertex[(i * 3)], m_vertex[(i * 3)+1], m_vertex[(i * 3)+2]);
	}
	glEnd();
	*/

	//Disable shader and vertex		
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(0);
	glUseProgram(0);

	modelview.pop();
}


//Draw Vertex of Object
void Object3D::drawVertex(Matrice &modelview, Matrice &projection)
{
	modelview.push();

	//Shader & Vertex Init
	glUseProgram(m_shaderColor.getProgramID());
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	//Shader Values
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "projection"), 1, GL_TRUE, projection.getValues());
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "modelview"), 1, GL_TRUE, modelview.getValues());
	
	//Draw Vertex
	glBegin(GL_POINTS);
	
	for(int i = 0; i < m_nbVertex; i++){
		glVertex3f(m_vertex[(i * 3)], m_vertex[(i * 3)+1], m_vertex[(i * 3)+2]);
	}
	glEnd();

	//Disable shader and vertex		
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(0);
	glUseProgram(0);

	modelview.pop();
}



void Object3D::Update(float time)
{

}